package com.gqj.elec.service.impl;

import com.gqj.common.utils.DateUtils;
import com.gqj.elec.domain.ElecMaterial;
import com.gqj.elec.domain.ElecMaterialIndetail;
import com.gqj.elec.domain.ElecMaterialIninfo;
import com.gqj.elec.mapper.ElecMaterialIndetailMapper;
import com.gqj.elec.mapper.ElecMaterialIninfoMapper;
import com.gqj.elec.mapper.ElecMaterialMapper;
import com.gqj.elec.service.IElecMaterialIndetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 物资入库详情Service业务层处理
 * 
 * @author lxq
 * @date 2023-12-17
 */
@Service
public class ElecMaterialIndetailServiceImpl implements IElecMaterialIndetailService 
{
    @Autowired
    private ElecMaterialIndetailMapper elecMaterialIndetailMapper;
    @Autowired
    private ElecMaterialMapper elecMaterialMapper;
    @Autowired
    private ElecMaterialIninfoMapper elecMaterialIninfoMapper;

    /**
     * 查询物资入库详情
     * 
     * @param indetailId 物资入库详情主键
     * @return 物资入库详情
     */
    @Override
    public ElecMaterialIndetail selectElecMaterialIndetailByIndetailId(Long indetailId)
    {
        return elecMaterialIndetailMapper.selectElecMaterialIndetailByIndetailId(indetailId);
    }

    /**
     * 查询物资入库详情列表
     * 
     * @param elecMaterialIndetail 物资入库详情
     * @return 物资入库详情
     */
    @Override
    public List<ElecMaterialIndetail> selectElecMaterialIndetailList(ElecMaterialIndetail elecMaterialIndetail)
    {
        return elecMaterialIndetailMapper.selectElecMaterialIndetailList(elecMaterialIndetail);
    }

    /**
     * 新增物资入库详情
     * 
     * @param elecMaterialIndetail 物资入库详情
     * @return 结果
     */
    @Override
    public int insertElecMaterialIndetail(ElecMaterialIndetail elecMaterialIndetail)
    {
        elecMaterialIndetail.setCreateTime(DateUtils.getNowDate());
        return elecMaterialIndetailMapper.insertElecMaterialIndetail(elecMaterialIndetail);
    }

    /**
     * 修改物资入库详情
     * 
     * @param elecMaterialIndetail 物资入库详情
     * @return 结果
     */
    @Override
    public int updateElecMaterialIndetail(ElecMaterialIndetail elecMaterialIndetail)
    {
        elecMaterialIndetail.setUpdateTime(DateUtils.getNowDate());
        return elecMaterialIndetailMapper.updateElecMaterialIndetail(elecMaterialIndetail);
    }

    /**
     * 批量删除物资入库详情
     * 
     * @param indetailIds 需要删除的物资入库详情主键
     * @return 结果
     */
    @Override
    public int deleteElecMaterialIndetailByIndetailIds(Long[] indetailIds)
    {
        return elecMaterialIndetailMapper.deleteElecMaterialIndetailByIndetailIds(indetailIds);
    }

    /**
     * 删除物资入库详情信息
     * 
     * @param indetailId 物资入库详情主键
     * @return 结果
     */
    @Override
    public int deleteElecMaterialIndetailByIndetailId(Long indetailId)
    {
        return elecMaterialIndetailMapper.deleteElecMaterialIndetailByIndetailId(indetailId);
    }

    /**
     * 执行物资入库
     *
     * @param elecMaterialIndetailList 物资入库详情集合
     * @return 结果
     */
    @Transactional
    public int elecMaterialInStorage(List<ElecMaterialIndetail> elecMaterialIndetailList){

        for(ElecMaterialIndetail elecMaterialIndetail: elecMaterialIndetailList){
            Long inDetailId = elecMaterialIndetail.getIndetailId();
            Long materialId = elecMaterialIndetail.getMaterialId();
            if(null == inDetailId ||  inDetailId == 0){
                continue;
            }
            if(null == materialId ||  materialId == 0){
                continue;
            }
            ElecMaterialIndetail detail= new ElecMaterialIndetail();
            detail.setIndetailId(inDetailId);
            detail.setIsIn("1");
            elecMaterialIndetailMapper.updateElecMaterialIndetail(detail);

            ElecMaterial elecMaterial = new ElecMaterial();
            elecMaterial.setMaterialId(materialId);
            elecMaterial.setMaterialStatus("1");
            elecMaterialMapper.updateElecMaterial(elecMaterial);
        }

        Long inId = elecMaterialIndetailList.get(0).getInId();
        ElecMaterialIndetail elecMaterialIndetail = new ElecMaterialIndetail();
        elecMaterialIndetail.setInId(inId);
        elecMaterialIndetail.setIsIn("0");
        List<ElecMaterialIndetail> elecMaterialIndetails = elecMaterialIndetailMapper.selectElecMaterialIndetailList(elecMaterialIndetail);
        if(null == elecMaterialIndetails || elecMaterialIndetails.size() == 0){
            ElecMaterialIninfo elecMaterialIninfo = new ElecMaterialIninfo();
            elecMaterialIninfo.setInId(inId);
            elecMaterialIninfo.setInStatus("3");
            elecMaterialIninfoMapper.updateElecMaterialIninfo(elecMaterialIninfo);
        } else {
            ElecMaterialIninfo elecMaterialIninfo = new ElecMaterialIninfo();
            elecMaterialIninfo.setInId(inId);
            elecMaterialIninfo.setInStatus("2");
            elecMaterialIninfoMapper.updateElecMaterialIninfo(elecMaterialIninfo);
        }
        return 0;
    }
}
